package org.csu.zhangdian.persistence;

import org.csu.zhangdian.dao.MemberInfoDAO;
import org.csu.zhangdian.domain.MemberInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

@Repository
public class MemberInfoDAOImpl implements MemberInfoDAO {

    @Autowired
    private DataSource dataSource;

    @Override
    public ArrayList<MemberInfo> findAll() {
        ArrayList<MemberInfo> infoList=new ArrayList<>();

        try {
            Connection connection=dataSource.getConnection();
            Statement statement=connection.createStatement();
            ResultSet resultSet=statement.executeQuery(FIND_ALL_MEMBERINFO);

            while (resultSet.next()){
                MemberInfo temp=new MemberInfo();
                temp.setID(resultSet.getInt("ID"));
                temp.setName(resultSet.getString("name"));
                temp.setSex(resultSet.getString("sex"));
                temp.setTelephone(resultSet.getInt("telephone"));
                temp.setPoint(resultSet.getInt("point"));
                temp.setLevel(resultSet.getInt("level"));
                temp.setBranch_no(resultSet.getInt("branch_no"));
                infoList.add(temp);
            }
            resultSet.close();
            statement.close();
            connection.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return infoList;
    }
}
